/*
    罗马数字转整数
    罗马数字包含以下七种字符: I， V， X， L，C，D 和 M。

    字符          数值
    I             1
    V             5
    X             10
    L             50
    C             100
    D             500
    M             1000

*/

#include <stdio.h>
#include <string.h>
int romanToint(char *s)
{
    int symbolValues[26];
    symbolValues['I'-'A'] = 1;
    symbolValues['V'-'A'] = 5;
    symbolValues['X'-'A'] = 10;
    symbolValues['L'-'A'] = 50;
    symbolValues['C'-'A'] = 100;
    symbolValues['D'-'A'] = 500;
    symbolValues['M'-'A'] = 1000;

    int ans = 0;
    int n = strlen(s);
    for(int i=0;i<n;i++)
    {
        int value = symbolValues[s[i]-'A'];
        if(i<n-1&&value<symbolValues[s[i+1]-'A'])
        {
            ans -= value;
        }
        else
        {
            ans += value;
        }
    }
    return ans;
}

int main(int argc, char const *argv[])
{
    char str[20] = {0};
    
    printf("请输入一个罗马数字: ");
    scanf("%s",str);

    int num = romanToint(str);

    printf("罗马数字转换后 %d \n",num);
    return 0;
}
